package com.bean.sys;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.module.MyDataTable;
import com.util.*;
import com.Constants;
import com.datasource.*;
import com.web.*;


public class RoleBeanbak
{
	private int roleId;
	
	private String roleName;
	
	private int roleType;
	
	private String roleDesc;
	
	private String creation;
	
	private String lastModify; 
	
	public List getSelectRoles() throws SQLException, UnsupportedEncodingException
	{
		StringBuffer sql = new StringBuffer("SELECT role_id as code_value,role_name as code_name FROM t_base_role");
		SQLQuery query = DSManager.getSQLQuery();
		List list = query.ListQuery( sql.toString() );
		return list;
	}
	
	public List getSelectRoles(UserBean ub) throws SQLException, UnsupportedEncodingException
	{
		StringBuffer sql = new StringBuffer("SELECT role_id as code_value,role_name as code_name " +
				"FROM t_base_role " +
				"where role_id in (select role_id from t_base_users_role where user_id="+ub.getId()+")");
		SQLQuery query = DSManager.getSQLQuery();
		List list = query.ListQuery( sql.toString() );
		return list;
	}
	
	public List getAllPermission() throws SQLException,UnsupportedEncodingException
	{
		String sql = "SELECT DISTINCT res_id,parent_id,res_name FROM t_base_function ";
		SQLQuery query = DSManager.getSQLQuery();
		return query.ListQuery(sql);
	}
	public List getRolePermission(int role_id) throws SQLException,UnsupportedEncodingException
	{
		String sql = "SELECT DISTINCT u.user_id,u.role_id,r.res_id,r.oper_level " +
				"FROM t_base_users_role u,t_base_role_function r " +
				"WHERE u.role_id=r.role_id and r.role_id="+role_id;
		SQLQuery query = DSManager.getSQLQuery();
		return query.ListQuery(sql);
	}
	
	public int getRolesCount() throws SQLException
	{
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_role");
		return query.getIntResult( sql.toString() );
	}
	
	public MyDataTable getRoles( int pageNumber, int pageSize ) throws SQLException
	{
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT * FROM t_base_role order by role_id");
		return query.DataTablePageQuery( sql.toString(), pageNumber, pageSize );
	}
	
	public boolean addPermission(int userId,int roleId,String [] resIds ) throws Exception
	{
		if( userId <= 0 || roleId <= 0 || resIds == null )
		{
			System.out.println(userId+"-----"+roleId);
			return false;
		}
		boolean flag = false;
		DSSession dss = DSManager.getDSSession();
		try
		{
			dss.beginTransaction();
			StringBuffer sql = new StringBuffer("DELETE FROM t_base_role_function WHERE role_id=?");
			Object [] prepareParam = new Object[1];
			prepareParam[0] =  roleId;
			dss.executePrepareUpdate( sql.toString(), prepareParam );
			//add
			sql = new StringBuffer("INSERT INTO t_base_role_function(add_id,role_id,res_id,oper_level) VALUES(?,?,?,?)");
			for(int i=1; i<resIds.length; i++ )
			{
				Object [] prepareParam2 = new Object[4];
				if(!"".equals(resIds[i])){
					//System.out.println(userId+"-----"+roleId+"-----"+ resIds[i]);
					prepareParam2[0] = userId ;
					prepareParam2[1] = roleId ;
					String[] res = resIds[i].split("@");
					prepareParam2[2] = res[0];
					if(res.length>1){
						prepareParam2[3] =  res[1];
					}else{
						prepareParam2[3] =  "";
					}
					dss.executePrepareUpdate( sql.toString(), prepareParam2 );
				}
			}
			dss.endTransaction();
			flag = true;
		}catch( Exception sqle )
		{
			sqle.printStackTrace();
			dss.rollback();
			//throw sqle;
		}finally
		{
			dss.close();
		}
		return flag;
	}
	public HashMap getRootRes() throws SQLException
	{
		String sql = "SELECT res_id,parent_id,res_name,node_type,model_id,position,link_value,remark,operation FROM t_base_function WHERE res_id = ? ";
		Object[] param = new Object[1];
		param[0] = new Integer( Constants.ROOT_RES_ID );
		SQLQuery query = DSManager.getSQLQuery();
		return query.getSingleRowPrepareResult( sql.toString(), param );		
	}
	
	public int add() throws SQLException
	{
		DSSession dss = DSManager.getDSSession();
		dss.beginTransaction();
		try
		{
			
			StringBuffer sql = new StringBuffer("INSERT INTO t_base_role(role_name,remark,add_date,lastmodify) VALUES(?,?,?,?)");
			Object [] prepareParam = new Object[4];
		
			prepareParam[0] = roleName;
//			prepareParam[2] = new Integer( roleType );
			prepareParam[1] = roleDesc;
			prepareParam[2] = DateHelper.getAppDateTime();
			prepareParam[3] = DateHelper.getAppDateTime();
			dss.executePrepareUpdate( sql.toString(), prepareParam );
			dss.endTransaction();
			
		}catch( SQLException sqle )
		{
			dss.rollback();
			throw sqle;
		}finally
		{
			dss.close();
		}
		return roleId;
	}
	
	public boolean update() throws SQLException
	{
		if( roleId <= 0 )
		{
			return false;
		}
		SQLUpdater updater = DSManager.getSQLUpdater();
		StringBuffer sql = new StringBuffer( "UPDATE t_base_role SET " )
			.append("role_name=?,")
//			.append("role_type=?,")
			.append("remark=?,")
			.append("lastmodify=? ")
			.append(" WHERE role_id=?");
		Object [] prepareParam = new Object[4];
		prepareParam[0] = roleName;
//		prepareParam[1] = new Integer( roleType );
		prepareParam[1] = roleDesc;
		prepareParam[2] = DateHelper.getAppDateTime();
		prepareParam[3] = new Integer( roleId );
		if( updater.executePrepareUpdate( sql.toString(), prepareParam ) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public boolean delete() throws SQLException
	{
		if( roleId <= 0 )
		{
			return false;
		}
		SQLUpdater updater = DSManager.getSQLUpdater();
		StringBuffer sql = new StringBuffer("DELETE FROM t_base_role WHERE role_id=?");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = new Integer( roleId );
		if( updater.executePrepareUpdate( sql.toString(), prepareParam ) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public boolean getById() throws SQLException
	{
		if( roleId <= 0 )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT * FROM t_base_role where role_id=").append( roleId );
		HashMap roleInfo = query.getSingleRowResult( sql.toString() );
		if( roleInfo != null )
		{
			roleName = StringHelper.getStringObjectValue( roleInfo.get("ROLE_NAME") );
//			roleType = StringHelper.getIntegerObjectValue( roleInfo.get("ROLE_TYPE") );
			roleDesc = StringHelper.getStringObjectValue( roleInfo.get("REMARK") );
			creation = StringHelper.getStringObjectValue( roleInfo.get("ADD_DATE") );
			lastModify = StringHelper.getStringObjectValue( roleInfo.get("LASTMODIFY") );
			return true;
		}
		return false;
	}
	
	public boolean exist() throws SQLException
	{
		if( roleName==null || "".equals(roleName) || "null".equals(roleName) )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_role WHERE role_name=? ");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = roleName;
		if ( query.getPrepareIntResult( sql.toString(), prepareParam ) > 0 )
		{
			return true;
		}
		return false;
	}
	
	public int exist( int role_id ) throws SQLException
	{
		if( roleId<=0 || roleName==null || "".equals(roleName) || "null".equals(roleName) )
		{
			return 0;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_role WHERE role_name=? AND role_id<>? ");
		Object [] prepareParam = new Object[2];
		prepareParam[0] = roleName;
		prepareParam[1] = new Integer( role_id );
		return query.getPrepareIntResult( sql.toString(), prepareParam );
	}

	public boolean isDelete() throws SQLException
	{
		if( roleId <= 0 )
		{
			return false;
		}
		SQLQuery query = DSManager.getSQLQuery();
		StringBuffer sql = new StringBuffer("SELECT count(*) FROM t_base_users_role WHERE role_id=?  ");
		Object [] prepareParam = new Object[1];
		prepareParam[0] = new Integer( roleId );
		if( query.getPrepareIntResult( sql.toString(), prepareParam ) > 0 )
		{
			return false;
		}
		return true;
	}
	
	public String getCreation()
	{
		return creation;
	}

	public void setCreation( String creation )
	{
		this.creation = creation;
	}

	public String getLastModify()
	{
		return lastModify;
	}

	public void setLastModify( String lastModify )
	{
		this.lastModify = lastModify;
	}

	public String getRoleDesc()
	{
		return roleDesc;
	}

	public void setRoleDesc( String roleDesc )
	{
		this.roleDesc = roleDesc;
	}

	public int getRoleId()
	{
		return roleId;
	}

	public void setRoleId( int roleId )
	{
		this.roleId = roleId;
	}

	public String getRoleName()
	{
		return roleName;
	}

	public void setRoleName( String roleName )
	{
		this.roleName = roleName;
	}

	public int getRoleType()
	{
		return roleType;
	}

	public void setRoleType( int roleType )
	{
		this.roleType = roleType;
	}
}
